package fr.babos.test.bundle 
{
	import com.bit101.components.InputText;
	import com.bit101.components.PushButton;
	import com.hasbro.listenoel.configs.HasbroLDNCommands;
	import com.hasbro.listenoel.controllers.GalleryController;
	import com.hasbro.listenoel.controllers.IntroductionController;
	import com.hasbro.listenoel.controllers.PagesController;
	import com.hasbro.listenoel.documents.HasbroLDNBase;
	import flash.display.Sprite;
	import flash.display.StageAlign;
	import flash.display.StageScaleMode;
	import flash.events.KeyboardEvent;
	import flash.ui.Keyboard;
	import flash.utils.setTimeout;
	import fr.babos.core.bootstrap.Bootstrap;
	import fr.babos.core.log.Log;
	import fr.babos.core.log.TraceLogger;
	import fr.babos.navigation.routes.Router;
	import fr.babos.test.mvc.BabosMyModel1;
	
	/**
	 * @author ZoulouX
	 */
	public class BundleTest extends Sprite 
	{
		/**
		 * Le routeur
		 */
		protected var _router				:Router;
		
		/**
		 * Le constructeur
		 */
		public function BundleTest ()
		{
			Log.addLogger(new TraceLogger());
			
			setTimeout(bundleTest, 200);
		}
		
		public function bundleTest ():void
		{
			
		}
		
		/**
		 * Initialiser le routeur
		 */
		public function initRouter ():void
		{
			Log.log("INIT ROUTER");
			
			// Créer le router
			_router = new Router();
			
			// Créer la barre d'adresse
			var addressField:InputText = new InputText(this, 10, 10);
			addressField.width = stage.stageWidth - 20;
			
			// Ecouter la barre d'adresse
			addressField.addEventListener(KeyboardEvent.KEY_DOWN, function addressKeyDownHandler (event:KeyboardEvent):void
			{
				// Si c'est la touche entrée on valide
				if (event.keyCode == Keyboard.ENTER)
					_router.url = addressField.text;
			});
			
			// Ecouter les changements sur le routeur
			_router.onLoadSuccess.addOnce(function ():void {
				Log.log("Router.onLoadSuccess");
				// Chargement des routes OK, on initialise l'interface
				initInterface();
			});
			_router.onRouteChange.add(function ():void
			{
				//Log.log("onRouteChange", _router.currentRoute);
			});
			_router.onRouteError.add(function ():void
			{
				//Log.log("onRouteError", _router.currentRoute);
			});
			_router.onURLChange.add(function ():void
			{
				//Log.log("onURLChange");
				
				// Actualiser l'adresse dans l'UI
				addressField.text = _router.url;
			});
			
			// Ajouter les routes
			_router.loadRoutes("xml/routes.xml");
		}
		
		/**
		 * Initialiser l'interface
		 */
		public function initInterface ():void
		{
			// Configurer le stage
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			// Ajouter les boutons pour changer de route
			(new PushButton(this, 10, 40, HasbroLDNCommands.INTRODUCTION_INDEX, function ():void {
				_router.open(HasbroLDNCommands.INTRODUCTION_INDEX);
			})).width = 100;
			
			(new PushButton(this, 120, 40, HasbroLDNCommands.HOMEPAGE_INDEX, function ():void {
				_router.open(HasbroLDNCommands.HOMEPAGE_INDEX);
			})).width = 100;
			
			(new PushButton(this, 230, 40, HasbroLDNCommands.GALLERY_LIST, function ():void {
				_router.open(HasbroLDNCommands.GALLERY_LIST, {
					id: (Math.random() * 100000).toString(16)
				});
			})).width = 100;
			
			(new PushButton(this, 340, 40, HasbroLDNCommands.GALLERY_INDEX, function ():void {
				_router.open(HasbroLDNCommands.GALLERY_INDEX);
			})).width = 100;
		}
		
		/**
		 * Test du bootstrap
		 */
		public function testBootstrap1 ():void
		{
			// Initialiser le routeur
			initRouter();
			
			// Créer le bootstrap et lui filer le container
			var bootstrap:Bootstrap = new Bootstrap(this);
			
			// Ecouter les changements d'URL
			_router.onURLChange.add(function ():void {
				bootstrap.open(_router.currentRoute.command, _router.currentParams);
			});
			
			// Tester une dépendence
			bootstrap.dependencesManager.addDependences(IntroductionController, {
				test: "HEY ! :)"
			});
			
			// Ajouter la commande de l'intro, la vue sera affichée dès que possible
			bootstrap.addCommand(
				HasbroLDNCommands.INTRODUCTION_INDEX,
				IntroductionController, "index",
				[View1Test],
				Bootstrap.ASAP
			);
			
			// Ajouter la commande de la homepage, attendre le controlleur
			bootstrap.addCommand(
				HasbroLDNCommands.HOMEPAGE_INDEX,
				PagesController, "index",
				[View1Test, View2Test],
				Bootstrap.WAIT_FOR_CONTROLLER
			);
			
			// Ajouter la commande de la gallerie, attendre toutes les vues
			bootstrap.addCommand(
				HasbroLDNCommands.GALLERY_LIST,
				GalleryController, "list",
				[View2Test, View3Test],
				Bootstrap.WAIT_FOR_ALL_VIEW
			);
			
			// Ajouter la commande de la gallerie, attendre toutes les vues
			bootstrap.addCommand(
				HasbroLDNCommands.GALLERY_INDEX,
				GalleryController, "index",
				[View1Test, View2Test, View3Test],
				Bootstrap.WAIT_FOR_ALL_VIEW
			);
		}
	}
}